package com.lk.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lk.springboot.pojo.UserToken;
import com.lk.springboot.service.LoginService;
import com.lk.springboot.utils.MD5Util;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author lk
 * @date 2020/1/8 0008 17:01
 * @description: 登录与首页模块
 */
@RestController
public class LoginController {

    @Autowired
    LoginService loginService;


    /**
     * 登录
     *
     * @param response
     * @param u_name
     * @param passwd
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpServletResponse response,
                        @RequestParam(value = "u_name", defaultValue = "noname") String u_name,
                        @RequestParam(value = "passwd", defaultValue = "000000") String passwd) {
        Result result = loginService.login(u_name, passwd);//登录结果
        if (result.getData().equals("faild")) {
            return JSON.toJSONString(result);//登录失败
        }
        MD5Util md5Util = new MD5Util();
        String MD5CookieValue = md5Util.encode(UUID.randomUUID().toString().replace("-", ""));
        Cookie cookie = new Cookie("token", MD5CookieValue);
        cookie.setMaxAge(60 * 60 * 24 * 10);
        response.addCookie(cookie);//颁发签名
        UserToken userToken = new UserToken(result.getData().toString(), MD5CookieValue);
        // System.out.println(userToken.getU_id() + "   " + userToken.getToken());
        loginService.saveToken(userToken);//存token到表里
        result.setData("success");
        return JSON.toJSONString(result);
    }

    /**
     * 获取主页信息
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = loginService.getIndex(u_id);
            return result;
        }

    }

    /**
     * 获取积分
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getmyscore", method = RequestMethod.GET)
    public String getuserscore(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = loginService.getmyscore(u_id);
            return result;
        }

    }

    /**
     * 获取用户已激活站点数量
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getmysitesnum", method = RequestMethod.GET)
    public String getusersites(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = loginService.getSites(u_id);
            return result;
        }

    }
}
